package com.louis.iread.bean;

import com.louis.iread.utils.DbUtil;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by louis on 17-11-1.
 */

public class BookMgr {
    /**
     * 书籍列表
     */
    private List<Book> mBooks;

    private static BookMgr mMgr = null;

    private BookMgr() {
        mBooks = getBooks();
    }

    public static BookMgr getMgr() {
        if (mMgr == null) {
            mMgr = new BookMgr();
        }
        return mMgr;
    }
    /**
     * 获取书籍列表
     * @return
     */
    public List<Book> getBooks() {
        if (mBooks != null && mBooks.size() > 0) {
            return mBooks;
        }
        try {

            List<Book> bookList = DbUtil.getDbManager().selector(Book.class).orderBy("readTime", true).findAll();
            if (bookList != null) {
                mBooks = bookList;
            } else {
                mBooks = new ArrayList<>();
            }
        } catch (DbException e) {
            e.printStackTrace();
            mBooks = new ArrayList<>();
        }
        return mBooks;
    }

    /**
     * 添加书籍
     * @param book
     * @return
     */
    public boolean addBook(Book book) {
        if (book == null) {
            return false;
        }
        try {
            //添加到数据库
            DbUtil.getDbManager().saveBindingId(book);
            mBooks.add(book);
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public Book getBook(long index) {
        for (Book book : mBooks) {
            if (book.getId() == index) {
                return book;
            }
        }
        return null;
    }

    /**
     * 更新书籍信息
     * @param book
     * @return
     */
    public boolean updateBook(Book book) {
        try {
            DbUtil.getDbManager().update(book);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 从书籍列表中删除书籍
     * @param index
     * @return
     */
    public void removeBook(long index) {
        for (Book book : mBooks) {
            if (book.getId() != index)
                continue;
            if (book != null) {
                book.removeArticleAll();
                try {
                    DbUtil.getDbManager().delete(book);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                mBooks.remove(book);
                break;
            }
        }
    }

    /**
     * 根据书籍url删除书籍
     * @param mainUrl
     */
    public void removeBook(String mainUrl) {
        if (mainUrl == null || mBooks == null) {
            return;
        }

        for (Book book : mBooks) {
            if (mainUrl.equals(book.getMainUrl())) {
                book.removeArticleAll();
                try {
                    DbUtil.getDbManager().delete(book);
                } catch (DbException e) {
                    e.printStackTrace();
                }
                mBooks.remove(book);
            }
        }
    }

    /**
     * 小说是否存在
     * @param mainUrl --小说主页url
     * @return
     */
    public boolean isBookExist(String mainUrl) {
        if (mainUrl == null || mainUrl.length() == 0 || mBooks == null || mBooks.size() == 0) {
            return false;
        }
        for (Book book : mBooks) {
            if (mainUrl.equals(book.getMainUrl())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 查找小说
     * @param mainUrl
     * @return
     */
    public Book getBook(String mainUrl) {
        if (mainUrl != null && mBooks != null) {
            for (Book book : mBooks) {
                if (mainUrl.equals(book.getMainUrl())) {
                    return book;
                }
            }
        }
        return null;
    }

    /**
     * 获取小说在书籍列表中的索引
     * @param mainUrl
     * @return
     */
    public long getBookId(String mainUrl) {
        if (mBooks == null || mBooks.size() == 0) {
            return -1;
        }

        for (int i = 0; i < mBooks.size(); i++) {
            if (mBooks.get(i).getMainUrl().equals(mainUrl)) {
                return mBooks.get(i).getId();
            }
        }
        return -1;
    }

    /**
     * 添加书籍章节目录
     * @param id
     * @param contents
     * @return
     */
    public boolean addArticles(long id, List<ContentItem> contents) {
        if (id < 0 || contents == null) {
            return false;
        }

        Book book = getBook(id);
        if (book != null) {
            return book.addArticles(contents);
        }
        return false;
    }
}
